'use strict';

const runScript = require('runscript');
const REGEX = /^\s*(\d+)\s+(.*)/;

exports.findNodeProcess = function* (filterFn) {
    const command = 'ps -eo "pid,command"';
    const stdio = yield runScript(command, { stdio: 'pipe' });
    const processList = stdio.stdout.toString().split('\n')
        .reduce((arr, line) => {
            if (!!line && !line.includes('/bin/sh') && line.includes('node')) {
                const m = line.match(REGEX);
                /* istanbul ignore else */
                if (m) {
                    const item = { pid: m[1], cmd: m[2] };
                    if (!filterFn || filterFn(item)) {
                        arr.push(item);
                    }
                }
            }
            return arr;
        }, []);
    return processList;
};

exports.kill = function (pids, signal) {
    pids.forEach(pid => {
        try {
            process.kill(pid, signal);
        } catch (err) { /* istanbul ignore next */
            if (err.code !== 'ESRCH') {
                throw err;
            }
        }
    });
};